import 'dart:convert';

import 'package:dio/dio.dart';
/**
 * 海报
 */
import 'package:flutter/material.dart';
import 'package:flutter_custom_bottom_tab_bar/eachtab.dart';
import 'package:flutter_custom_bottom_tab_bar/tabbar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:panda/base/SignType.dart';
import 'package:panda/commuity/PlayBillChild.dart';
import 'package:panda/http/Api.dart';
import 'package:panda/http/HttpUtil.dart';
import 'package:panda/util/SignUtil.dart';

import '../base/Constant.dart';

class PlayBillPage extends StatefulWidget {
  PlayBillPage({Key key}) : super(key: key);

  @override
  _PlayBillPageState createState() => _PlayBillPageState();
}

class _PlayBillPageState extends State<PlayBillPage>
    with SingleTickerProviderStateMixin {
  TabController _tabController;
  List<String> names = new List();
  List<String> ids = new List();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    loadList();
  }

  loadList() async {
    Map map = new Map();
    map['pid'] = "8";
    map['sign_type'] = SignType.GET_SUB_CATE_LIST;
    Response<String> response =
        await HttpUtil().post(Api.GET_SUBCATE_LIST, data: getSign(map));
    print(response.data);
    Map userMap = json.decode(response.data);
    if (userMap['code'] == 1 && mounted) {
      (userMap['data']['list'] as List).forEach((v) {
        // datas.add(new HomeListDataData.fromJson(v));
        names.add(v['cat_name']);
        ids.add(v['cat_id'].toString());
      });
      _tabController =
          new TabController(vsync: this, initialIndex: 0, length: names.length);
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
          title: Text('宣传海报',
              style: TextStyle(
                  fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
                  color: Colors.black)),
          backgroundColor: Colors.white,
          brightness: Brightness.light,
          centerTitle: true,
          iconTheme: new IconThemeData(color: Colors.black),
          elevation: 0.5,
          bottom: TabContainer(
              child: Column(
            // overflow: Overflow.clip,
            children: <Widget>[
              Container(
                height: 1,
                color: f0Color,
              ),
              names.length == 0
                  ? Text('')
                  : Container(
                      alignment: Alignment.centerLeft,
                      child: new TabBar(
                        isScrollable: true,
                        controller: _tabController,
                        indicatorPadding: EdgeInsets.fromLTRB(
                            ScreenUtil.getInstance().setHeight(DP_30),
                            0,
                            ScreenUtil.getInstance().setHeight(DP_30),
                            0),
                        indicatorColor: mainColor,
                        labelColor: mainColor,
                        indicatorWeight: 3,
                        labelPadding: EdgeInsets.all(0),
                        unselectedLabelColor: Colors.black,
                        tabs: creatScope(),
                      ),
                    )
            ],
          ))),
      body: names.length == 0
          ? Text('')
          : TabBarView(controller: _tabController, children: creatScope2()),
    );
  }

  creatScope() {
    var result = new List<Widget>();
    for (int i = 0; i < names.length; i++) {
      result.add(EachTab(
        padding: EdgeInsets.fromLTRB(
            ScreenUtil.getInstance().setWidth(NORMAL_FONT_SIZE), 0, 10, 0),
        text: names[i],
        textStyle: TextStyle(
            fontSize: ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
      ));
    }
    return result;
  }

  creatScope2() {
    var result = new List<Widget>();
    for (int i = 0; i < ids.length; i++) {
      result.add(new PlayBillChildPage(
        id: ids[i],
      ));
    }
    return result;
  }
}
